package spring.yongongsystem.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import spring.yongongsystem.POJO.dto.AdminDTO;
import spring.yongongsystem.POJO.dto.AdminPageQueryDTO;
import spring.yongongsystem.POJO.entity.Admin;
import spring.yongongsystem.POJO.vo.AdminLoginVO;
import spring.yongongsystem.constant.JwtClaimsConstant;
import spring.yongongsystem.properties.JwtProperties;
import spring.yongongsystem.result.PageResult;
import spring.yongongsystem.result.Result;
import spring.yongongsystem.service.AdminService;
import spring.yongongsystem.utils.JwtUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@CrossOrigin(origins = "http://localhost:5173")
@RestController
@RequestMapping("/admin/admin")
@Slf4j
@Api(tags = "管理员个人相关接口")
public class AdminController {

    String key = "admin_token";
    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param adminDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "管理员登录")
    public Result<AdminLoginVO> login(@RequestBody AdminDTO adminDTO) {
        log.info("员工登录：{}", adminDTO);

        Admin admins = adminService.login(adminDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, admins.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .id(admins.getId())
                .userName(admins.getUsername())
                .token(token)
                .build();
        redisTemplate.opsForValue().set(key,token);
        return Result.success(adminLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("管理员退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param adminDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增管理员")
    public Result save(@RequestBody AdminDTO adminDTO){
        log.info("新增管理员：{}",adminDTO);
        adminService.save(adminDTO);
        return Result.success();
    }

    /**
     * 员工分页查询
     * @param adminPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("管理员分页查询")
    public Result<PageResult> page(AdminPageQueryDTO adminPageQueryDTO){
        log.info("管理员分页查询，参数为：{}", adminPageQueryDTO);
        PageResult pageResult = adminService.pageQuery(adminPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用管理员账号")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启用禁用管理员账号：{},{}",status,id);
        adminService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询管理员信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询管理员信息")
    public Result<Admin> getById(@PathVariable Long id){
        log.info("查询管理员账号：{}",id);
        Admin admins = adminService.getById(id);
        return Result.success(admins);
    }

    /**
     * 编辑管理员信息
     * @param adminDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑管理员信息")
    public Result update(@RequestBody AdminDTO adminDTO){
        log.info("编辑管理员信息：{}", adminDTO);
        adminService.update(adminDTO);
        return Result.success();
    }
}

